package com.hncc.sms.service.impl;

import java.util.List;

import com.hncc.sms.domain.SmsSkdRule;
import com.hncc.sms.domain.SmsSkdRuleItem;
import com.hncc.sms.mapper.SmsSkdRuleItemMapper;
import com.hncc.sms.mapper.SysUserManagerMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import com.hncc.sms.mapper.SmsSkdRuleMapper;
import com.hncc.sms.service.ISmsSkdRuleService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 排班规则Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-08-08
 */
@Service
public class SmsSkdRuleServiceImpl implements ISmsSkdRuleService 
{
    @Resource
    private SmsSkdRuleMapper smsSkdRuleMapper;

    @Resource
    private SysUserManagerMapper sysUserManagerMapper;

    @Resource
    private SmsSkdRuleItemMapper smsSkdRuleItemMapper;

    /**
     * 查询排班规则
     * 
     * @param id 排班规则ID
     * @return 排班规则
     */
    @Override
    public SmsSkdRule selectSmsSkdRuleById(Long id)
    {
        return smsSkdRuleMapper.selectSmsSkdRuleById(id);
    }

    /**
     * 查询排班规则列表
     * 
     * @param smsSkdRule 排班规则
     * @return 排班规则
     */
    @Override
    public List<SmsSkdRule> selectSmsSkdRuleList(SmsSkdRule smsSkdRule)
    {
        return smsSkdRuleMapper.selectSmsSkdRuleList(smsSkdRule);
    }

    /**
     * 新增排班规则
     * 
     * @param smsSkdRule 排班规则
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSmsSkdRule(SmsSkdRule smsSkdRule)
    {
        //根据登录名查询登录id
        Long userId = sysUserManagerMapper.checkUserNameUnique(SecurityUtils.getUsername());
        //操作人
        smsSkdRule.setOperatorId(userId);
        smsSkdRule.setOperateTime(DateUtils.getNowDate());
        //先添加排班规则
        smsSkdRuleMapper.insertSmsSkdRule(smsSkdRule);
        int result = 0 ;
        //再添加排班时间
        for(int i = 0; i<smsSkdRule.getSmsSkdRuleItems().size();i++){
            SmsSkdRuleItem smsSkdRuleItem = smsSkdRule.getSmsSkdRuleItems().get(i);
            smsSkdRuleItem.setSkRuleId(smsSkdRule.getId());
            result = smsSkdRuleItemMapper.insertSmsSkdRuleItem(smsSkdRuleItem);
        }
        return result;
    }

    /**
     * 修改排班规则
     * 
     * @param smsSkdRule 排班规则
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSmsSkdRule(SmsSkdRule smsSkdRule)
    {//根据登录名查询登录id
        Long userId = sysUserManagerMapper.checkUserNameUnique(SecurityUtils.getUsername());
        //操作人
        smsSkdRule.setOperatorId(userId);
        //先修改排版规则
        int result = smsSkdRuleMapper.updateSmsSkdRule(smsSkdRule);
        //在删除排版规则相关的id
        smsSkdRuleItemMapper.deleteSmsSkdRuleItemBySkRuleId(smsSkdRule.getId());
        //最后添加排班时间
        for(int i = 0; i<smsSkdRule.getSmsSkdRuleItems().size();i++){
            SmsSkdRuleItem smsSkdRuleItem = smsSkdRule.getSmsSkdRuleItems().get(i);
            smsSkdRuleItem.setSkRuleId(smsSkdRule.getId());
            result = smsSkdRuleItemMapper.insertSmsSkdRuleItem(smsSkdRuleItem);
        }
        return result;
    }

    /**
     * 批量删除排班规则
     * 
     * @param ids 需要删除的排班规则ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSmsSkdRuleByIds(Long[] ids)
    {
        //先根据排班规则id批量删除排班规则明细管理表
        smsSkdRuleItemMapper.deleteSmsSkdRuleItemBySkRuleIds(ids);
        //在批量删除排班规则表
        return smsSkdRuleMapper.deleteSmsSkdRuleByIds(ids);
    }

    /**
     * 删除排班规则信息
     * 
     * @param id 排班规则ID
     * @return 结果
     */
    @Override
    public int deleteSmsSkdRuleById(Long id)
    {
        return smsSkdRuleMapper.deleteSmsSkdRuleById(id);
    }
}
